package com.weiyin.examination.course;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.weiyin.examination.R;
import com.weiyin.examination.application.Constant;
import com.weiyin.examination.listener.CourseInterface;
import com.weiyin.examination.listener.DialogShowListener;
import com.weiyin.examination.module.AnswerModule;
import com.weiyin.examination.module.TestModule;
import com.weiyin.examination.util.LayoutHelper;
import com.weiyin.examination.util.LogUtil;
import com.weiyin.examination.util.SPUtil;
import com.weiyin.examination.view.SurfaceViewTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author njf
 *         Created by lenovo on 2017/9/19.
 */

public class CourseA8 extends Course {
    private View rootView;
    private Activity activity;
    private ClickAble clickAble;

    private TextView titleLayoutNextTextView, titleLayoutLastTextView, titleLayoutSubjectContentTextView,
            titleLayoutSubjectScoreTextView, titleLayoutNumberTextView, titleTicketNumber,
            courseA8TextView2, courseA8TextView1,
            courseA8TextViewContent2, courseA8TextViewContent1;
    private RelativeLayout titleLayoutNumberRl;
    private SurfaceViewTemplate courseA8SurfaceView2, courseA8SurfaceView1;

    private TestModule testModule;
    private List<TestModule> testModuleList;
    private List<AnswerModule> answerModuleList;
    private int courseIndex;
    private Handler mHandler = new Handler();
    private double score;

    @Override
    public void onCreate(Activity activity, ViewFlipper contain, List<TestModule> courseEntities, int index) {
        if (rootView == null) {
            rootView = LayoutInflater.from(activity).inflate(R.layout.coursea8_layout, null);

            LayoutHelper layoutHelper = new LayoutHelper(activity);
            layoutHelper.scaleView(rootView);

            contain.addView(rootView);
            this.testModuleList = courseEntities;
            this.activity = activity;
            this.courseIndex = index;
            this.testModule = testModuleList.get(index);
            clickAble = new ClickAble();
            if (testModule != null) {
                if (testModule.getRightAnswerList() != null) {
                    this.answerModuleList = testModule.getRightAnswerList();
                }
            }

            initView();
        }
    }

    @Override
    public void initView() {
        titleLayoutNextTextView = (TextView) rootView.findViewById(R.id.titleLayoutNextTextView);
        titleLayoutLastTextView = (TextView) rootView.findViewById(R.id.titleLayoutLastTextView);
        titleLayoutSubjectContentTextView = (TextView) rootView.findViewById(R.id.titleLayoutSubjectContentTextView);
        titleLayoutSubjectScoreTextView = (TextView) rootView.findViewById(R.id.titleLayoutSubjectScoreTextView);
        titleLayoutNumberTextView = (TextView) rootView.findViewById(R.id.titleLayoutNumberTextView);
        courseA8TextView1 = (TextView) rootView.findViewById(R.id.courseA8TextView1);
        courseA8TextView2 = (TextView) rootView.findViewById(R.id.courseA8TextView2);
        courseA8TextViewContent1 = (TextView) rootView.findViewById(R.id.courseA8TextViewContent1);
        courseA8TextViewContent2 = (TextView) rootView.findViewById(R.id.courseA8TextViewContent2);
        titleTicketNumber = (TextView) rootView.findViewById(R.id.titleTicketNumber);
        courseA8SurfaceView1 = (SurfaceViewTemplate) rootView.findViewById(R.id.courseA8SurfaceView1);
        courseA8SurfaceView2 = (SurfaceViewTemplate) rootView.findViewById(R.id.courseA8SurfaceView2);

        titleLayoutNumberRl = (RelativeLayout) rootView.findViewById(R.id.titleLayoutNumberRl);

        titleLayoutNextTextView.setOnClickListener(clickAble);
        titleLayoutLastTextView.setOnClickListener(clickAble);
        titleLayoutNumberRl.setOnClickListener(clickAble);

        courseA8TextView1.setOnClickListener(clickAble);
        courseA8TextView2.setOnClickListener(clickAble);
    }

    private void initViewData() {
        if (testModule != null) {
            titleTicketNumber.setText("准考证号：" + "" + testModule.getTickNumber());

            titleLayoutLastTextView.setVisibility(courseIndex == 0 ? View.GONE : View.VISIBLE);
            titleLayoutNextTextView.setVisibility(courseIndex == testModuleList.size() - 1 ? View.GONE : View.VISIBLE);

            titleLayoutSubjectContentTextView.setText((courseIndex + 1) + "丶" + testModule.getSubjectTitle());
            titleLayoutSubjectScoreTextView.setText("（" + testModule.getScore() + "分）");
            titleLayoutNumberTextView.setText("第" + (courseIndex + 1) + "题/第" + (testModuleList.size()) + "题");

            courseA8TextView1.setText(testModule.getSelectionList().get(0).getSelectContent());
            courseA8TextViewContent1.setText(testModule.getSelectionList().get(0).getSelectContent());
            courseA8TextView2.setText(testModule.getSelectionList().get(1).getSelectContent());
            courseA8TextViewContent2.setText(testModule.getSelectionList().get(1).getSelectContent());
        }
    }

    class ClickAble implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.titleLayoutNextTextView:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    courseInterface.courseInterfaceResult(Constant.NEXT_KNOWLEDGE, courseIndex + 1);
                    break;
                case R.id.titleLayoutLastTextView:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    courseInterface.courseInterfaceResult(Constant.LAST_KNOWLEDGE, courseIndex - 1);
                    break;
                case R.id.courseA8TextView1:
                    courseA8SurfaceView1.setVisibility(View.VISIBLE);
                    courseA8TextViewContent1.setVisibility(View.VISIBLE);
                    courseA8TextView1.setVisibility(View.GONE);
                    break;
                case R.id.courseA8TextView2:
                    courseA8SurfaceView2.setVisibility(View.VISIBLE);
                    courseA8TextViewContent2.setVisibility(View.VISIBLE);
                    courseA8TextView2.setVisibility(View.GONE);
                    break;
                case R.id.titleLayoutNumberRl:
                    dialogShowListener.dialogShow(1, courseIndex, result());
                    dialogShowListener.dialogShow(0, courseIndex, result());
                    break;
                default:
                    break;
            }
        }
    }

    private void getList(String str, List<String> data) {
        if (str.contains(Constant.SUBJECT_URL_CONTAINS)) {
            String s = str.substring(0, str.indexOf(Constant.SUBJECT_URL_CONTAINS));
            data.add(s);
            Log.i(TAG, s + "|" + str.substring(str.indexOf(Constant.SUBJECT_URL_CONTAINS) + 1));
            getList(str.substring(str.indexOf(Constant.SUBJECT_URL_CONTAINS) + 1), data);
        } else {
            data.add(str);
        }
    }

    private boolean result() {

        Constant.getScreen(activity, SPUtil.getSharedPreferencesData(activity, "number") + "_" + (courseIndex + 1),SPUtil.getSharedPreferencesData(activity, "number")+"_"+ (courseIndex + 1));
        List<String> result = new ArrayList<>();
        List<String> result1 = new ArrayList<>();
        List<String> answer = new ArrayList<>();
        List<String> answer1 = new ArrayList<>();
        score = 0;

        getList(answerModuleList.get(0).getAnswerContentList().get(0), answer);
        if (answerModuleList.size() > 1)
            getList(answerModuleList.get(1).getAnswerContentList().get(0), answer1);
        if (courseA8SurfaceView1.getmColors() != null) {
            for (int i = 0; i < courseA8SurfaceView1.getmColors().length; i++) {
                if (courseA8SurfaceView1.getmColors()[i] == courseA8SurfaceView1.getSelectBackground()) {
                    result.add(courseA8SurfaceView1.getmStarsWai()[i]);
                }
            }
        }
        if (courseA8SurfaceView1.getmColorsCenter() != null) {
        for (int i = 0; i < courseA8SurfaceView1.getmColorsCenter().length; i++) {
            if (courseA8SurfaceView1.getmColorsCenter()[i] == courseA8SurfaceView1.getSelectBackground()) {
                result.add(courseA8SurfaceView1.getmStars()[i]);
            }
        }}
        if (courseA8SurfaceView2.getmColors() != null) {
        for (int i = 0; i < courseA8SurfaceView2.getmColors().length; i++) {
            if (courseA8SurfaceView2.getmColors()[i] == courseA8SurfaceView2.getSelectBackground()) {
                result1.add(courseA8SurfaceView2.getmStarsWai()[i]);
            }
        }}

        if (courseA8SurfaceView2.getmColorsCenter() != null) {
            for (int i = 0; i < courseA8SurfaceView2.getmColorsCenter().length; i++) {
                if (courseA8SurfaceView2.getmColorsCenter()[i] == courseA8SurfaceView2.getSelectBackground()) {
                    result1.add(courseA8SurfaceView2.getmStars()[i]);
                }
            }
        }
        if (result.size() <= answer.size()) {
            for (int i = 0; i < result.size(); i++) {
                if (answer.contains(result.get(i))) {
                    score += Double.valueOf(answerModuleList.get(0).getScore()) / answer.size();
                }
            }
        }
        if (result1.size() <= answer1.size()) {
            for (int i = 0; i < result1.size(); i++) {
                if (answer1.contains(result1.get(i))) {
                    score += Double.valueOf(answerModuleList.get(1).getScore()) / answer1.size();
                }
            }
        }

        LogUtil.i(TAG,"score="+score);
        SPUtil.setSharedPreferencesData(activity.getApplication(), CourseA8.class.getSimpleName(), score + "");

        if (courseA8SurfaceView1.getmColors() != null) {
            for (int i = 0; i < courseA8SurfaceView1.getmColors().length; i++) {
                if (courseA8SurfaceView1.getmColors()[i] == courseA8SurfaceView1.getSelectBackground()) {
                    return true;
                }
            }
        }
        if (courseA8SurfaceView1.getmColorsCenter() != null) {
            for (int i = 0; i < courseA8SurfaceView1.getmColorsCenter().length; i++) {
                if (courseA8SurfaceView1.getmColorsCenter()[i] == courseA8SurfaceView1.getSelectBackground()) {
                    return true;
                }
            }
        }
        if (courseA8SurfaceView2.getmColors() != null) {
            for (int i = 0; i < courseA8SurfaceView2.getmColors().length; i++) {
                if (courseA8SurfaceView2.getmColors()[i] == courseA8SurfaceView2.getSelectBackground()) {
                    return true;
                }
            }
        }
        if (courseA8SurfaceView2.getmColorsCenter() != null) {
            for (int i = 0; i < courseA8SurfaceView2.getmColorsCenter().length; i++) {
                if (courseA8SurfaceView2.getmColorsCenter()[i] == courseA8SurfaceView2.getSelectBackground()) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void setCourseInterface(CourseInterface courseInterface) {
        this.courseInterface = courseInterface;
    }

    @Override
    public void setCourseDialogListener(DialogShowListener dialogListener) {
        this.dialogShowListener = dialogListener;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    public void show(List<TestModule> courseEntities, int index, String type) {
        initViewData();
    }

    @Override
    public void stop() {

    }

    @Override
    public void destroy() {

    }
}
